import json
import numpy as np
import os
import matplotlib.pyplot as plt
from collections import Counter

#初始化一个保存类别信息的列表
key_class_list_vechile = []
key_class_list_road = []
key_class_list_location = []
key_class_list_weather = []
key_class_list_season = []
key_class_list_time = []
key_class_list_indoor = []

def vechile(files):
    #读取标注信息并写入 xml
    for json_file_ in files:
        json_filename = os.path.join(labelme_path, json_file_) + ".json"
        json_file = json.load(open(json_filename, "r", encoding="gb2312"))
        
        vechile = json_file["data_collection_vechile"]
        key_class_list_vechile.append((vechile))
        count_vechile = Counter(key_class_list_vechile)
        vechile = list(count_vechile)
        vechile=np.array(vechile)
        num_vechile = count_vechile.values()
        # print(num_vechile)

    #刻度距离坐标轴的距离调整
    plt.tick_params(pad = 0.03)  #通过pad参数调整距离

    #data_collection_vechile
    plt.bar(vechile, num_vechile, color = 'g', align =  'center')
    plt.text(float(len(vechile)/2 - 0.5), max(num_vechile)*1.15, list(num_vechile), ha='center', fontsize=12)
    plt.text(float(len(vechile)/2 - 0.5), max(num_vechile)*1.25, "imgs of dataset is %s"% len(files), ha='center', fontsize=12)
    plt.title('Bosch 2D Detection, num_vechile') 
    plt.ylabel('Num axis',fontsize=11) 
    plt.xlabel('Vechile axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
    plt.savefig('num_vechile.png', bbox_inches='tight')
    plt.close()

def road(files):
    #读取标注信息并写入 xml
    for json_file_ in files:
        json_filename = os.path.join(labelme_path, json_file_) + ".json"
        json_file = json.load(open(json_filename, "r", encoding="gb2312"))
        
        road = json_file["data_collection_road"]
        key_class_list_road.append((road))
        count_road = Counter(key_class_list_road)
        road = list(count_road)
        road=np.array(road)
        num_road = count_road.values()
        # print(num_road)

    #刻度距离坐标轴的距离调整
    plt.tick_params(pad = 0.03)  #通过pad参数调整距离

    #data_collection_road
    plt.bar(road, num_road, color = 'g', align =  'center')
    plt.text(float(len(road)/2 - 0.5), max(num_road)*1.15, list(num_road), ha='center', fontsize=12)
    plt.text(float(len(road)/2 - 0.5), max(num_road)*1.25, "imgs of dataset is %s"% len(files), ha='center', fontsize=12)
    plt.title('Bosch 2D Detection') 
    plt.ylabel('Num axis',fontsize=11) 
    plt.xlabel('Road axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
    plt.savefig('num_road.png', bbox_inches='tight')
    plt.close()

def location(files):
    #读取标注信息并写入 xml
    for json_file_ in files:
        json_filename = os.path.join(labelme_path, json_file_) + ".json"
        json_file = json.load(open(json_filename, "r", encoding="gb2312"))
        
        location = json_file["data_collection_location"]
        key_class_list_location.append((location))
        count_location = Counter(key_class_list_location)
        location = list(count_location)
        location=np.array(location)
        num_location = count_location.values()

    print('num of location is %s'% location.size)
    print('location is %s'% location)
    print('num_location is %s'% num_location)
    #刻度距离坐标轴的距离调整
    plt.tick_params(pad = 0.03)  #通过pad参数调整距离

    #data_collection_location
    plt.barh(location, num_location, color = 'g', align =  'center')
    plt.text(max(num_location)/2, len(location)*1.15, list(num_location), ha='center', fontsize=12)
    plt.text(max(num_location)/2, len(num_location)*1.25, "imgs of dataset is %s"% len(files), ha='center', fontsize=12)
    plt.title('Bosch 2D Detection') 
    plt.ylabel('Location axis',fontsize=11) 
    plt.xlabel('Num axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
    plt.savefig('num_location.png', bbox_inches='tight')
    plt.close()

def weather(files):
    #读取标注信息并写入 xml
    for json_file_ in files:
        json_filename = os.path.join(labelme_path, json_file_) + ".json"
        json_file = json.load(open(json_filename, "r", encoding="gb2312"))
        
        weather = json_file["data_collection_weather"]
        key_class_list_weather.append((weather))
        count_weather = Counter(key_class_list_weather)
        weather = list(count_weather)
        weather=np.array(weather)
        num_weather = count_weather.values() 

    #刻度距离坐标轴的距离调整
    plt.tick_params(pad = 0.03)  #通过pad参数调整距离

    #data_collection_weather
    plt.bar(weather, num_weather, color = 'g', align =  'center')
    plt.text(float(len(weather)/2 - 0.5), max(num_weather)*1.15, list(num_weather), ha='center', fontsize=12)
    plt.text(float(len(weather)/2 - 0.5), max(num_weather)*1.25, "imgs of dataset is %s"% len(files), ha='center', fontsize=12)
    plt.title('Bosch 2D Detection') 
    plt.ylabel('Num axis',fontsize=11) 
    plt.xlabel('Weather axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
    plt.savefig('num_weather.png', bbox_inches='tight')
    plt.close()

def season(files):
    #读取标注信息并写入 xml
    for json_file_ in files:
        json_filename = os.path.join(labelme_path, json_file_) + ".json"
        json_file = json.load(open(json_filename, "r", encoding="gb2312"))
        
        season = json_file["data_collection_season"]
        key_class_list_season.append((season))
        count_season = Counter(key_class_list_season)
        season = list(count_season)
        season=np.array(season)
        num_season = count_season.values()
    
    #刻度距离坐标轴的距离调整
    plt.tick_params(pad = 0.03)  #通过pad参数调整距离

    #data_collection_season
    plt.bar(season, num_season, color = 'g', align =  'center')
    plt.text(float(len(season)/2 - 0.5), max(num_season)*1.15, list(num_season), ha='center', fontsize=12)
    plt.text(float(len(season)/2 - 0.5), max(num_season)*1.25, "imgs of dataset is %s"% len(files), ha='center', fontsize=12)
    plt.title('Bosch 2D Detection') 
    plt.ylabel('Num axis',fontsize=11) 
    plt.xlabel('Season axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
    plt.savefig('num_season.png', bbox_inches='tight')
    plt.close()

def time(files):
    #读取标注信息并写入 xml
    for json_file_ in files:
        json_filename = os.path.join(labelme_path, json_file_) + ".json"
        json_file = json.load(open(json_filename, "r", encoding="gb2312"))
        
        time = json_file["data_collection_time"]
        key_class_list_time.append((time))
        count_time = Counter(key_class_list_time)
        time = list(count_time)
        time=np.array(time)
        num_time = count_time.values()
        

    #刻度距离坐标轴的距离调整
    plt.tick_params(pad = 0.03)  #通过pad参数调整距离

    #data_collection_season
    plt.barh(time, num_time, color = 'g', align =  'center')
    plt.text(max(num_time)/2, len(num_time)*1.15, list(num_time), ha='center', fontsize=12)
    plt.text(max(num_time)/2, len(num_time)*1.25, "imgs of dataset is %s"% len(files), ha='center', fontsize=12)
    plt.title('Bosch 2D Detection') 
    plt.ylabel('Time axis',fontsize=11) 
    plt.xlabel('Num axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
    plt.savefig('num_time.png', bbox_inches='tight')
    plt.close()

def indoor(files):
    #读取标注信息并写入 xml
    for json_file_ in files:
        json_filename = os.path.join(labelme_path, json_file_) + ".json"
        json_file = json.load(open(json_filename, "r", encoding="gb2312"))
        
        if 'data_collection_indoor' in json_file:
            indoor = json_file["data_collection_indoor"]
            # print(json_filename)
            key_class_list_indoor.append((indoor))
            count_indoor = Counter(key_class_list_indoor)
            indoor = list(count_indoor)
            indoor=np.array(indoor)
            num_indoor = count_indoor.values()


    #刻度距离坐标轴的距离调整
    plt.tick_params(pad = 0.03)  #通过pad参数调整距离

    #data_collection_season
    plt.bar(indoor, num_indoor, color = 'g', align =  'center')
    plt.text(float(len(indoor)/2 - 0.5), max(num_indoor)*1.15, list(num_indoor), ha='center', fontsize=12)
    plt.text(float(len(indoor)/2 - 0.5), max(num_indoor)*1.25, "imgs of dataset is %s"% len(files), ha='center', fontsize=12)
    plt.title('Bosch 2D Detection') 
    plt.ylabel('Num axis',fontsize=11) 
    plt.xlabel('Indoor axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
    plt.savefig('num_indoor.png', bbox_inches='tight')
    plt.close()


if __name__ == "__main__":
    #标签路径
    labelme_path = "all_json_104806_20220729"   #原始labelme标注数据路径
    #获取待处理文件
    files = [p.split('.json')[0] for p in os.listdir(labelme_path) if '.json' in p]
    print("there are %s files"%len(files))
    vechile(files)
    road(files)
    location(files)
    weather(files)
    season(files)
    time(files)
    indoor(files)